﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PdfLibrary
{
    public class PdfDictionaryEntry
    {
        public PdfName Name;
        public IPdfObject Value;
    }

    public class PdfDictionary : Dictionary<string, IPdfObject>, IPdfObject
    {
        public PdfDictionary(){ }
        public PdfDictionary(PdfReader reader){ LoadFromReader(reader, true); }

        public bool LoadFromReader(PdfReader reader, bool throwException)
        {
            reader.ParseDirection = FileReaderParseDirection.Forward;

            if (!reader.TryReadToken("<<"))
                if (throwException) throw new PdfParsingException(reader, "Dictionary start tag (<<) not found!");
                else return false;

            // because of nested dictionaries, we delete this instance from the singleton dictionary
            SingletonDictionary.Reset<PdfDictionary>();

            while (!reader.TryReadToken(">>"))
            {
                reader.PositionCheck("searching for dictionary end");

                this.Add(PdfName.GetValueFromReader(reader), PdfObject.CreateFromReader(reader));
            }

            return true;
        }
    }
}
